// src/main.js
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from './router/indexMy.js'

const app = createApp(App);

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

// 添加路由守卫
router.beforeEach((to, from, next) => {
    // 如果访问的是登录页或注册页，直接放行
    if (to.path === '/login' || to.path === '/register' || to.path === '/password-reset') {
        next()
        return
    }

    // 检查是否有token
    const token = localStorage.getItem('token')
    if (token) {
        next() // 有token，允许访问
    } else {
        next('/login') // 无token，跳转到登录页
    }
})

app.use(router);
app.use(ElementPlus);
app.mount('#app')